package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TypeMapper;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.service.ITypeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 品种类别 服务层实现
 * 
 * @author ruoyi
 * @date 2019-04-22
 */
@Service
public class TypeServiceImpl implements ITypeService 
{
	@Autowired
	private TypeMapper typeMapper;

	/**
     * 查询品种类别信息
     * 
     * @param typeId 品种类别ID
     * @return 品种类别信息
     */
    @Override
	public Type selectTypeById(Long typeId)
	{
	    return typeMapper.selectTypeById(typeId);
	}
	
	/**
     * 查询品种类别列表
     * 
     * @param type 品种类别信息
     * @return 品种类别集合
     */
	@Override
	public List<Type> selectTypeList(Type type)
	{
	    return typeMapper.selectTypeList(type);
	}
	
    /**
     * 新增品种类别
     * 
     * @param type 品种类别信息
     * @return 结果
     */
	@Override
	public int insertType(Type type)
	{
	    return typeMapper.insertType(type);
	}
	
	/**
     * 修改品种类别
     * 
     * @param type 品种类别信息
     * @return 结果
     */
	@Override
	public int updateType(Type type)
	{
	    return typeMapper.updateType(type);
	}

	/**
     * 删除品种类别对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTypeByIds(Long ids)
	{
		return typeMapper.deleteTypeById(ids);
	}

	@Override
	public String checkTypeNameUnique(Type type) {
		Long typeId = StringUtils.isNull(type.getTypeId()) ? -1L : type.getTypeId();
		Type info = typeMapper.checkTypeNameUnique(type.getTypeName(), type.getParentId());
		if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
		{
			return UserConstants.DEPT_NAME_NOT_UNIQUE;
		}
		return UserConstants.DEPT_NAME_UNIQUE;
	}

	@Override
	public List<Ztree> selectTypeTree(Type type) {
		List<Type> typeList = typeMapper.selectTypeList(type);
		List<Ztree> ztrees = initZtree(typeList);
		return ztrees;
	}

	@Override
	public int selectTypeCount(Long typeId) {
		Type type = new Type();
		type.setParentId(typeId);
		return typeMapper.selectTypeCount(type);
	}

	@Override
	public Boolean checkTypeExistInventory(Long typeId) {
		int result = typeMapper.checkTypeExistInventory(typeId);
		return result > 0 ? true : false;
	}

	public List<Ztree> initZtree(List<Type> typeList)
	{
		return initZtree(typeList, null);
	}

	/**
	 * 对象转部门树
	 *
	 * @param roleDeptList 角色已存在菜单列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<Type> typeList, List<String> roleDeptList)
	{

		List<Ztree> ztrees = new ArrayList<Ztree>();
		boolean isCheck = StringUtils.isNotNull(roleDeptList);
		for (Type type : typeList)
		{
			Ztree ztree = new Ztree();
			ztree.setId(type.getTypeId());
			ztree.setpId(type.getParentId());
			ztree.setName(type.getTypeName());
			ztree.setTitle(type.getTypeName());
			if (isCheck)
			{
				ztree.setChecked(roleDeptList.contains(type.getTypeId() + type.getTypeName()));
			}
			ztrees.add(ztree);
		}
		return ztrees;
	}

}
